package GUI;

import javax.swing.JPanel;
import javax.swing.JLabel;
import java.awt.Font;
import javax.swing.JList;
import javax.swing.JButton;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;

import java.awt.SystemColor;
import javax.swing.border.LineBorder;
import java.awt.Color;
import java.util.ArrayList;

import logic.Message;
import logic.SystemMesseges;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.UIManager;

public class SystemmessageGui extends JPanel{
	private JTextField textField;
	private Client_GUI parent;
	
	private JList list;
	private SystemMesseges msg;
	private JButton btnDeleteMessage;
	private SystemmessageGui self;
	private SystemMesseges selected;
	private int selectedIndex = -1;
	private ArrayList<SystemMesseges> privateMessages;
	
	public SystemmessageGui(final Client_GUI parent) {
		setBackground(UIManager.getColor("inactiveCaption"));
		self = this;
		setLayout(null);
		this.parent = parent;
		this.setSize(Client_GUI.MAIN_WIDTH, Client_GUI.MAIN_HEIGHT);
				
		privateMessages = parent.client.privateMessages;
		
		JLabel lblMessageCenter = new JLabel("Message Center");
		lblMessageCenter.setFont(new Font("Tahoma", Font.BOLD, 14));
		lblMessageCenter.setBounds(329, 11, 149, 22);
		add(lblMessageCenter);
		
		list = new JList();
		list.setBackground(UIManager.getColor("control"));
		list.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent arg0) {
				
				selectedIndex = list.getSelectedIndex();
				
				if (list.getSelectedIndex() != -1) {
					selected = privateMessages.get(list.getSelectedIndex());

					btnDeleteMessage.setEnabled(true);
					textField.setText(selected.getMESSAGE());
					
					if (selected.getREAD() == SystemMesseges.MESSAGE_UNREAD){
						//TODO set the select font to normal
						
						int action = Message.READ_MESSAGE_REQUEST;
						
						ArrayList<String> index = new ArrayList<String>();
						ArrayList<Object> data = new ArrayList<Object>();
						index.add("MID");
						data.add(selected.getMID());
						
						Message toSend = new Message(index, data, action);
						
						if (parent.sendMessageToServer(toSend) != 1){//message was not sent successfully
							//TODO verify extream situation
						}	
						
					}
					
				}else{
					btnDeleteMessage.setEnabled(false);
					textField.setText("");
				}
				
				parent.safeToClear = false;
				
			}
		});
		list.setBorder(new LineBorder(new Color(0, 0, 0), 2));
		list.setBounds(66, 50, 682, 248);
		add(list);
		
		btnDeleteMessage = new JButton("Delete Message");
		btnDeleteMessage.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) 
			{
				//TODO SET VERIFY DIALOG FOR DELETING
				
				textField.setText("");
				int action = Message.DELETE_MESSAGE_REQUEST;
				
				ArrayList<String> index = new ArrayList<String>();
				ArrayList<Object> data = new ArrayList<Object>();
				index.add("MID");
				data.add(selected.getMID());
				Message toSend = new Message(index, data, action);
				
				if (parent.sendMessageToServer(toSend) == 1){//message sent successfully
					parent.client.privateMessages.remove(selectedIndex);
					LoadResults();
					selectedIndex = -1;
				}else{//message send failed
					//TODO verify extream situation
				}
			}
		});
		btnDeleteMessage.setBounds(132, 463, 156, 40);
		add(btnDeleteMessage);
		btnDeleteMessage.setEnabled(false);
		
		JButton btnDhone = new JButton("Done");
		btnDhone.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				int priv = parent.client.Privelege;
				
				parent.RepaintMenu(priv);
			}
		});
		btnDhone.setBounds(537, 463, 156, 40);
		add(btnDhone);
		
		textField = new JTextField();
		textField.setBackground(SystemColor.activeCaption);
		textField.setBounds(66, 323, 682, 129);
		add(textField);
		textField.setColumns(10);
		textField.setEnabled(false);
		
		JLabel lblSelectMessage = new JLabel("Select message");
		lblSelectMessage.setFont(new Font("Tahoma", Font.BOLD, 12));
		lblSelectMessage.setBounds(66, 34, 118, 14);
		add(lblSelectMessage);
		
		LoadResults();
		
	}
	
	
	
	public void LoadResults(){
		
		ArrayList<String> listData = new ArrayList<String>();
		
		if (privateMessages == null) return;
			
		for (SystemMesseges msg : privateMessages){
			String toDisplay = msg.getDATE() + "    -    " + msg.getTITLE();
			
			if(msg.getREAD() == SystemMesseges.MESSAGE_UNREAD){
				//TODO if type = 0 set font to bold
			}
			
			listData.add(toDisplay);
		}

		list.setListData(listData.toArray());
		
	}


}
